package org.tech4j.springboot.starter.edi.persistence.jdbc;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tech4j.edi.persistence.api.RetryConfigPersistService;
import org.tech4j.edi.persistence.api.RetryRecordPersistService;
import org.tech4j.edi.persistence.jdbc.JdbcRetryConfigPersistService;
import org.tech4j.edi.persistence.jdbc.JdbcRetryRecordPersistService;
import org.tech4j.springboot.starter.edi.persistence.common.EdiPersistenceConfiguration;

import javax.sql.DataSource;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/10 11:02
 **/
@Configuration
@AutoConfigureAfter(DataSource.class)
@ConditionalOnMissingBean(EdiPersistenceConfiguration.class)
public class JdbcEdiPersistenceConfiguration extends EdiPersistenceConfiguration {

    @Bean
    @ConditionalOnBean(DataSource.class)
    @ConditionalOnMissingBean(RetryRecordPersistService.class)
    public RetryRecordPersistService jdbcRetryRecordPersistService(final DataSource dataSource) {
        return new JdbcRetryRecordPersistService(dataSource);
    }

    @Bean
    @ConditionalOnBean(DataSource.class)
    @ConditionalOnMissingBean(RetryConfigPersistService.class)
    public RetryConfigPersistService jdbcRetryConfigPersistService(final DataSource dataSource) {
        return new JdbcRetryConfigPersistService(dataSource);
    }
}
